package com.playfair.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.playfair.core.AppCore;
import com.playfair.thread.utils.BruteForceAttackResult;

public class BruteForceAttackDialog extends JFrame {

	private static final long serialVersionUID = 6882272311005793038L;
	private JLabel codeLabel, nmbOfThreadsLabel;
	private JTextArea codeTextArea;
	private JButton confirmButton, cancelButton;
	private JScrollPane codeScrollPane;
	private JPanel nmbOfThreadsPanel;
	private JRadioButton threeThreads, fourThreads, fiveThreads;
	private Integer nmbOfThreads;

	public BruteForceAttackDialog() {
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Brute Force attack");
		setLayout(new GridLayout(3, 2));
		setContent();
		pack();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		setResizable(false);
	}

	private void setContent() {
		nmbOfThreads = new Integer(3);
		codeLabel = new JLabel("Code");
		codeTextArea = new JTextArea(5, 10);
		codeTextArea.setLineWrap(true);
		codeScrollPane = new JScrollPane(codeTextArea);
		codeScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		cancelButton = new JButton("Cancel");
		confirmButton = new JButton("Attack!");

		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});

		confirmButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				long start = System.nanoTime();
				// ArrayList<BruteForceAttackResult> result = AppCore
				// .getInstance().owenAttack(
				// codeTextArea.getText().trim(), nmbOfThreads);
				ArrayList<BruteForceAttackResult> result = AppCore
						.getInstance().owenAttackTrigram(
								codeTextArea.getText().trim(), nmbOfThreads);

				String info = "Time taken: " + (System.nanoTime() - start)
						/ 1000000000 + "s\nBest result: \n   "
						+ result.get(0).getResult()
						+ "\nOther possible results:";
				for (int i = 1; i < result.size(); i++)
					info += "\n   " + result.get(i).getResult();
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(null, info);
			}
		});

		threeThreads = new JRadioButton("3");
		fourThreads = new JRadioButton("4");
		fiveThreads = new JRadioButton("5");

		threeThreads.setSelected(true);
		fourThreads.setSelected(false);
		fiveThreads.setSelected(false);

		threeThreads.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				fourThreads.setSelected(false);
				fiveThreads.setSelected(false);
				nmbOfThreads = 3;
			}
		});

		fourThreads.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				threeThreads.setSelected(false);
				fiveThreads.setSelected(false);
				nmbOfThreads = 4;
			}
		});

		fiveThreads.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				threeThreads.setSelected(false);
				fourThreads.setSelected(false);
				nmbOfThreads = 5;
			}
		});

		nmbOfThreadsPanel = new JPanel();
		nmbOfThreadsPanel.add(threeThreads);
		nmbOfThreadsPanel.add(fourThreads);
		nmbOfThreadsPanel.add(fiveThreads);

		nmbOfThreadsLabel = new JLabel("Threads");

		add(codeLabel);
		add(codeScrollPane);
		add(nmbOfThreadsLabel);
		add(nmbOfThreadsPanel);
		add(confirmButton);
		add(cancelButton);
	}
}
